/*****************************************************************************
Module :     ThreadSchedule.H
Notices:     2007.3.1 Written by Ling, Xiao-li.  Mailto: ling.xiaoli.s@gmail.com
Description: This file encapsulates schedule of travian tasks in a extra thread.
			 
*****************************************************************************/
#pragma once

#include "EventLog.h"
//#include "Travian.h"
#include "TravianTaskSchedule.h"

using namespace System::EventLog;

#define WM_THREADMSG_SCHEDULE_SETPARAM		( WM_APP + 105 )	// Sent from UI window to set schedule parameters before task start. LPARAM is the pointer of parameter data. But user can set action choice anytime in main UI).
#define WM_THREADMSG_SCHEDULE_INIT			( WM_APP + 106 )	// Sent from UI window to init schedule.
#define WM_THREADMSG_SCHEDULE_START			( WM_APP + 107 )	// Sent from UI window to start schedule. 
#define WM_THREADMSG_SCHEDULE_STOP			( WM_APP + 108 )	// Sent from UI window to stop schedule. 
#define WM_THREADMSG_SCHEDULE_STARTATTACK	( WM_APP + 109 )	// Sent from UI window to start attack alert schedule. 
#define WM_THREADMSG_SCHEDULE_CLEAN_ATTACK	( WM_APP + 110 )	// Sent from UI window to start attack alert schedule. 
#define WM_THREADMSG_SCHEDULE_ERASE_ATTACK	( WM_APP + 111 )	// Sent from UI window to erase one attack entry 

// CThreadSchedule

//namespace Travian
//{

class CThreadSchedule : public CWinThread
{
	DECLARE_DYNCREATE(CThreadSchedule)

protected:
	CThreadSchedule();           // protected constructor used by dynamic creation
	virtual ~CThreadSchedule();

public:
	struct ScheduleParam
	{
	public:
		HWND						hWnd;				// Field 0
		CString						sSvr;				// Field 1
		CString						sUser;				// Field 2
		CString						sPwd;				// Field 3
		CString						sTaskFileName;		// Field 4
		Tasks						ts;					// Field 5
		eTravianTaskFinishAction	eAction;			// Field 6
		CEventLog*					pEvtLog;			// Field 7
		unsigned long				ulFieldSetFlag;
	public:
		ScheduleParam(): ulFieldSetFlag(0) {};
		ScheduleParam(ScheduleParam& rhs) {
			hWnd				= rhs.hWnd;
			sSvr				= rhs.sSvr;
			sUser				= rhs.sUser;
			sPwd				= rhs.sPwd;
			sTaskFileName		= rhs.sTaskFileName;
			ts					= rhs.ts;
			eAction				= rhs.eAction;
			pEvtLog				= rhs.pEvtLog;
			ulFieldSetFlag		= rhs.ulFieldSetFlag;
		};
		~ScheduleParam() {};

		void SetWnd(HWND hWnd)								{ ulFieldSetFlag |= (1<<0); this->hWnd = hWnd; };
		void SetSvr(LPCTSTR lpszSvr)						{ ulFieldSetFlag |= (1<<1); this->sSvr = lpszSvr; };
		void SetUserPwd(LPCTSTR lpszUser, LPCTSTR lpszPwd)	{ ulFieldSetFlag |= (1<<2); ulFieldSetFlag |= (1<<3); this->sUser = lpszUser;  this->sPwd = lpszPwd; };
		void SetTaskFileName(LPCTSTR lpszTaskFileName)		{ ulFieldSetFlag |= (1<<4); this->sTaskFileName = lpszTaskFileName; };
		void SetTasks(Tasks& ts)							{ ulFieldSetFlag |= (1<<5); this->ts = ts; };
		void SetAction(eTravianTaskFinishAction eAction)	{ ulFieldSetFlag |= (1<<6); this->eAction = eAction; };
		void SetEventLog(CEventLog* pEvtLog)				{ ulFieldSetFlag |= (1<<7); this->pEvtLog = pEvtLog; };
		void SetAttackTasks(Tasks& ts)						{ ulFieldSetFlag |= (1<<8); this->ts = ts; };
	};

	void GetAttackDateStr(CString &sAttackDate) const { m_TTS.GetAttackDateStr(sAttackDate); }
	CTravianTaskSchedule &GetTTS() { return m_TTS; }

	// wParam is the pointer of parameter object
	afx_msg void OnScheduleInit(WPARAM wParam, LPARAM lParam);
	afx_msg void OnScheduleSetParam(WPARAM wParam, LPARAM lParam);
	afx_msg void OnScheduleStart(WPARAM wParam, LPARAM lParam);
	afx_msg void OnScheduleStop(WPARAM wParam, LPARAM lParam);
	afx_msg void OnScheduleStartAttackTimer(WPARAM wParam, LPARAM lParam);
	afx_msg void OnCleanAttack(WPARAM wParam, LPARAM lParam);
	afx_msg void OnEreaseAttackTimer(WPARAM wParam, LPARAM lParam);
	

protected:
	CTravianTaskSchedule	m_TTS;

public:
	virtual BOOL InitInstance();
	virtual int ExitInstance();

protected:
	DECLARE_MESSAGE_MAP()
};

//}
